package com.xpgk.mvc.right;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantInit;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.dto.ZtreeNode;
import com.xpgk.mvc.base.BaseService;

@Service(name = RightService.serviceName)
public class RightService extends BaseService {

	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(RightService.class);

	public static final String serviceName = "rightService";

	/**
	 * 获取子节点数据
	 * 
	 * @param cxt
	 * @param parentIds
	 * @return
	 */
	public List<ZtreeNode> childNodeData(String cxt, Map<String, Object> queryParam) {
		List<Record> list = null;
		if (null != queryParam.get(Right.column_ids)) {
			queryParam.put(Right.column_parentids, queryParam.get(Right.column_ids));
			list = find(Right.sqlId_childNode, queryParam);

		} else {
			String sql = getSqlMy(Right.sqlId_rootNode);
			list = Db.find(sql);
		}

		List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
		ZtreeNode node = null;

		for (Record right : list) {
			node = new ZtreeNode();
			node.setId(right.getStr(Right.column_ids));
			node.setName(right.getStr(Right.column_name));
			node.setIsParent(right.getStr(Right.column_isparent));
			node.setIcon(cxt + ConstantRender.icon_path + right.getStr(Right.column_icon));
			nodeList.add(node);
		}

		return nodeList;
	}

	/**
	 * 保存
	 * 
	 * @param pIds
	 * @param names
	 * @param orderIds
	 * @return
	 */
	public RenderBean addRight(Right right) {
		RenderBean result = new RenderBean();
		Right pRight = Right.dao.findById(right.getStr(Right.column_parentids));
		pRight.set(Right.column_isparent, "true").update();

		right.set(Right.column_isparent, "false");
		right.set(Right.column_level, right.getInt(Right.column_level) + 1);
		right.save();
		result.setCode(ConstantRender.render_success_code);
		return result;
	}

	/**
	 * 更新
	 * 
	 * @param ids
	 * @param names
	 */
	public RenderBean update(Right right) {
		RenderBean result = new RenderBean();
		Right parentRight = Right.dao.findById(right.getStr(Right.column_parentids));
		parentRight.set(Right.column_isparent, "true").update();
		right.update();
		result.setCode(ConstantRender.render_success_code);
		return result;
	}

	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	public String delete(String ids) {
		Right right = Right.dao.findById(ids);

		// 是否存在子节点
		if (right.getStr(Right.column_isparent).equals("true")) {
			return "存在子模块，不能直接删除";
		}

		String countSql = getSqlMy(Right.sqlId_childCount);
		long count = Db.use(ConstantInit.db_dataSource_main).queryNumber(countSql, ids).longValue();
		if (count != 0) {
			return "模块下存在功能，不能删除"; // 部门下存在人，不能删除
		}

		// 修改上级节点的isparent
		Right pRight = Right.dao.findById(right.getStr(Right.column_parentids));
		if (null != pRight) {
			String sql = getSqlMy(Right.sqlId_childCount);
			Record record = Db.use(ConstantInit.db_dataSource_main).findFirst(sql, pRight.getPKValue());
			Long counts = record.getNumber("counts").longValue();
			if (counts == 1) {
				pRight.set(Right.column_isparent, "false");
				pRight.update();
			}
		}
		// 删除
		Right.dao.deleteById(ids);

		return ConstantRender.render_success;
	}

}
